#include"DoubleLinkedList.h"
#include<iostream>

///打印DoubleLinkedList<int>类的链表
void printList(DoubleLinkedList<int> &_list)
{
    DoubleLinkedList<int>::iterator itr=_list.begin();
    std::cout<<"["<<*(itr++);
    for(;itr!=_list.end();++itr)
    {
	std::cout<<","<<*itr;
   }
    std::cout<<"]"<<std::endl;
}

///测试改进后的迭代器错误时的异常处理
void test()
{
    DoubleLinkedList<int>list1;
    DoubleLinkedList<int>list2;
    for(int i=1;i<=5;i++)
	list1.push_back(i);
    for(int i=1;i<=5;i++)
	list2.push_back(10-i);

    DoubleLinkedList<int>::iterator itr1;
    DoubleLinkedList<int>::iterator itr2=list2.begin();
    DoubleLinkedList<int>::iterator itr3=list2.end();
    try
    {
	list1.erase(itr1);///<未初始化的迭代器
	list1.insert(itr1,6);
    }
    catch(IteratorOutOfBoundsException s1){}
    try
    {
        list1.erase(itr2);///<错误的迭代器
	list1.insert(itr2,6);
    }
    catch(IteratorMismatchException s2){}
    try
    {
        list2.erase(itr3,itr2);///<迭代器范围输入错误
    }
    catch(IteratorOutOfBoundsException s3){}
    printList(list1);
    printList(list2);
}

int main(int argc,char *argv[])
{
    DoubleLinkedList<int> L;
    for(int i=1;i<=5;i++)
	L.push_back(i);

    printList(L);
    
    DoubleLinkedList<int>::iterator iter=L.find(L,3);
    if(iter!=L.end())///<判断是否找到3,若找到再进行删除操作
    {
	L.erase(iter);
    }
    
    printList(L);
    iter=L.find(L,3);

    test();///调用测试函数
    
    return 0;
}
